from django.db import models
from django.apps import apps


class EvalQuestion(models.Model):
    question_id = models.AutoField(primary_key=True, verbose_name="问题ID")
    item = models.CharField(max_length=10, verbose_name="题目代码")
    question = models.CharField(max_length=200, verbose_name="题目内容")
    weight = models.IntegerField(verbose_name="权重")

    class Meta:
        verbose_name_plural = "评测问题"
        verbose_name = "评测问题"
        db_table = "zy_eval_question"


class EvalList(models.Model):
    # ClassInfo = apps.get_model(
    #     "zy_manage", "ClassInfo"
    # )  # 避免两个模型循环引入，使用字符串路径引用模型
    # CourseInfo = apps.get_model(
    #     "zy_manage", "CourseInfo"
    # )  # 避免两个模型循环引入，使用字符串路径引用模型
    # TeacherInfo = apps.get_model(
    #     "zy_manage", "TeacherInfo"
    # )  # 避免两个模型循环引入，使用字符串路径引用模型
    # StudentInfo = apps.get_model(
    #     "zy_manage", "StudentInfo"
    # )  # 避免两个模型循环引入，使用字符串路径引用模型

    eval_id = models.AutoField(primary_key=True, verbose_name="清单ID")
    class_id = models.ForeignKey(
        "zy_manage.ClassInfo",
        on_delete=models.CASCADE,
        verbose_name="班级ID",
        default=None,
    )
    class_name = models.CharField(max_length=20, verbose_name="班级名称", default="")
    course_id = models.ForeignKey(
        "zy_manage.CourseInfo",
        on_delete=models.CASCADE,
        verbose_name="课程ID",
        default=None,
    )
    course_name = models.CharField(max_length=20, verbose_name="课程名称", default="")
    teacher_id = models.ForeignKey(
        "zy_manage.TeacherInfo",
        on_delete=models.CASCADE,
        verbose_name="教师ID",
        default=None,
    )
    teacher_name = models.CharField(max_length=20, verbose_name="教师姓名", default="")

    score1 = models.IntegerField(verbose_name="分数1")
    score2 = models.IntegerField(verbose_name="分数2")
    score3 = models.IntegerField(verbose_name="分数3")
    score4 = models.IntegerField(verbose_name="分数4")
    score5 = models.IntegerField(verbose_name="分数5")
    score6 = models.IntegerField(verbose_name="分数6")
    score7 = models.IntegerField(verbose_name="分数7")
    score8 = models.IntegerField(verbose_name="分数8")
    score = models.FloatField(verbose_name="总分", default=0)

    student_id = models.ForeignKey(
        "zy_manage.StudentInfo",
        on_delete=models.CASCADE,
        verbose_name="学生ID",
        default=None,
    )
    student_name = models.CharField(max_length=20, verbose_name="学生姓名", default="")
    eval_date = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name_plural = "评测清单"
        verbose_name = "评测清单"
        db_table = "zy_eval_list"

    def update_score(self):
        score1 = self.score1 * EvalQuestion.objects.get(item="score1").weight
        score2 = self.score2 * EvalQuestion.objects.get(item="score2").weight
        score3 = self.score3 * EvalQuestion.objects.get(item="score3").weight
        score4 = self.score4 * EvalQuestion.objects.get(item="score4").weight
        score5 = self.score5 * EvalQuestion.objects.get(item="score5").weight
        score6 = self.score6 * EvalQuestion.objects.get(item="score6").weight
        score7 = self.score7 * EvalQuestion.objects.get(item="score7").weight
        score8 = self.score8 * EvalQuestion.objects.get(item="score8").weight
        self.score = (
            score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8
        ) / 100

    def save(self, *args, **kwargs):
        self.update_score()
        super(EvalList, self).save(*args, **kwargs)

    def __str__(self):
        return self.eval_id
